package com.jk.controller;

import com.alipay.api.AlipayApiException;
import com.jk.service.AlipayService;
import com.jk.util.AlipayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 类描述：
 *
 * @ClassName PayController
 * @Description TODO
 * @Author fandasen
 * @Date 2022/6/7 17:52
 * @Version 1.0
 */
@Controller
@RequestMapping("alipay")
public class PayController {

    @Autowired
    private AlipayService alipayService;

    //创建订单提交订单
    @RequestMapping("topay")
    public String topay(){
        return "map";
    }

    //创建订单提交订单
    @RequestMapping("index2")
    public String index2(){
        return "index2";
    }

    //支付失败
    @ResponseBody
    @RequestMapping("failure")
    public String topay1(){
        return "支付失败";
    }
    //支付成功
    @ResponseBody
    @RequestMapping("success")
    public String topay2(){
        return "支付成功";
    }

    //支付
    @ResponseBody
    @RequestMapping("pay1")
    public String toPay(@RequestParam("WIDout_trade_no") String order_number, @RequestParam("WIDtotal_amount") String total_amount, HttpServletResponse response, HttpServletRequest request) throws IOException, AlipayApiException {
        String pays = alipayService.pay(order_number,total_amount,response,request);
        //支付成功添加日志
        AlipayConfig.logResult(pays);
        return pays;
    }

    //退款
    @ResponseBody
    @RequestMapping("refund")
    public String toRefund(HttpServletResponse response, HttpSession session,String out_trade_no,String refund_amount) throws IOException, AlipayApiException {
        String pay = alipayService.refund1(response,session,out_trade_no,refund_amount);
//        退款成功添加日志
        AlipayConfig.logResult(pay);
        return pay;
    }
}
